//
//  RSloadingHub.m
//  SRHUB
//
//  Created by 王保霖 on 16/4/7.
//  Copyright © 2016年 Ricky. All rights reserved.
//

#import "RSloadingHub.h"

@interface RSloadingHub(){


    BOOL animating;
    CGFloat cornerRadius;
    CGRect originFrame;
    
}

@property(nonatomic,strong)UIView * progressView;
@property(nonatomic,assign)CGFloat progressHight;
@property(nonatomic,assign)CGFloat progressWidth;

@end

@implementation RSloadingHub

-(instancetype)initWithFrame:(CGRect)frame{

    if(self = [super initWithFrame:frame]){

        
        _progressHight = frame.size.height/2;
        _progressWidth = frame.size.width*2;
      
    }
    
      return  self;
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{\

    
    originFrame = self.frame;
    
    if(animating == YES){
        
        
        
        return;
    }
    
    animating = YES;

    for (CALayer *layer in self.layer.sublayers) {
        
        [layer removeFromSuperlayer];
    }

    self.backgroundColor = [UIColor blueColor];
    
    
    self.layer.cornerRadius = self.progressHight/2;
    
    
    CABasicAnimation * radiusAnimation = [CABasicAnimation animationWithKeyPath:@"cornerRadius"];
    
    radiusAnimation.duration = 0.5f;
    
    radiusAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    
    radiusAnimation.fromValue = @(originFrame.size.height/2);
    
    radiusAnimation.delegate = self;
    
    [self.layer addAnimation:radiusAnimation forKey:@"cornerRadiusShrinkAnim"];
 

}

-(void)animationDidStart:(CAAnimation *)anim{


    if([anim isEqual:[self.layer animationForKey:@"cornerRadiusShrinkAnim"]]){
    
        [UIView animateWithDuration:0.6f delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
           
            
            self.bounds = CGRectMake(0, 0, _progressWidth, _progressHight);
            
        } completion:^(BOOL finished) {

            [self.layer removeAllAnimations];
            
            [self progressBarAnimation];
        }];
        
        
        
    }
    
    
    if([anim isEqual:[self.layer animationForKey:@"cornerRadiusExpandAnim"]]){
        
        [UIView animateWithDuration:0.6f delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            
            
            self.bounds = CGRectMake(0, 0,originFrame.size.width, originFrame.size.height);
            
            self.backgroundColor = [UIColor colorWithRed:0.18 green:0.8 blue:0.443 alpha:1];
            
            
        } completion:^(BOOL finished) {
            
            [self.layer removeAllAnimations];
            
            [self checkAnimation];
            
            animating = NO;
        }];
        
        
        
    }
    
    
}

-(void)checkAnimation{

    CAShapeLayer *checkLayer = [CAShapeLayer layer];
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    CGRect rectInCircle = CGRectInset(self.bounds,self.bounds.size.width*(1-1/sqrt(2.0))/2, self.bounds.size.width*(1-1/sqrt(2.0))/2);
    
    [path moveToPoint:CGPointMake(rectInCircle.origin.x+rectInCircle.size.width/9, rectInCircle.origin.y+rectInCircle.size.height*2/3)];
    
    [path addLineToPoint:CGPointMake(rectInCircle.origin.x+rectInCircle.size.width/3, rectInCircle.origin.y+rectInCircle.size.height*9/10)];
    
        [path addLineToPoint:CGPointMake(rectInCircle.origin.x+rectInCircle.size.width*8/10, rectInCircle.origin.y+rectInCircle.size.height*2/10)];
    
    checkLayer.path = path.CGPath;
    
    checkLayer.lineWidth = 10.0;
    
    checkLayer.lineJoin = kCALineJoinRound;
    
    checkLayer.lineCap = kCALineCapRound;
    
    checkLayer.fillColor = [UIColor clearColor].CGColor;
    
    checkLayer.strokeColor = [UIColor whiteColor].CGColor;
    
    [self.layer addSublayer:checkLayer];
    
    
    CABasicAnimation *pathAnimaton = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnimaton.duration = 0.3f;
    pathAnimaton.fromValue = @(0.0f);
    pathAnimaton.toValue = @(1.0f);
    
    pathAnimaton.delegate = self;
    
    [pathAnimaton setValue:@"CheckAnimation" forKey:@"animatonName"];
    
    [checkLayer addAnimation:pathAnimaton forKey:nil];
    
    

}

-(void)progressBarAnimation{
    
    CAShapeLayer *progressLayer = [CAShapeLayer layer];
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    [path moveToPoint:CGPointMake(_progressHight/2,self.bounds.size.height/2)];
    
    [path addLineToPoint:CGPointMake(self.bounds.size.width - _progressHight/2, self.bounds.size.height/2)];
    
    
    progressLayer.path = path.CGPath;
    
    progressLayer.strokeColor = [UIColor whiteColor].CGColor;
    
    progressLayer.lineWidth = _progressHight-6;
    
    
    /**
     *  lineCap:封口的样式
     *  1. kCALineCapButt : 默认的样式 不附加任何的形状
     *  2. kCALineCapRound : 在线段的头和尾添加lineWidth一半的半圆
     *  3. kCALineCapSquare :在线段的头和尾添加lineWidth一半的矩形
     */
    
    progressLayer.lineCap = kCALineCapRound;
    
    
    [self.layer addSublayer:progressLayer];
  
    CABasicAnimation *pathAnimaton = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
      pathAnimaton.duration = 2.0f;
    pathAnimaton.fromValue = @(0.0f);
    pathAnimaton.toValue = @(1.0f);
  
    pathAnimaton.delegate = self;
    
    [pathAnimaton setValue:@"progressBarAnimation" forKey:@"animatonName"];
    
    [progressLayer addAnimation:pathAnimaton forKey:nil];

}

-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{

    if([[anim valueForKey:@"animatonName"] isEqualToString:@"progressBarAnimation"]){
    
        [UIView animateWithDuration:0.3 animations:^{
            
            for (CALayer *sublayer in self.layer.sublayers) {
            
                sublayer.opacity = 0.0f;
            }
            
        } completion:^(BOOL finished) {
            
            if(finished){
            
                
                for (CALayer *sublayer in self.layer.sublayers) {
                    
                    [sublayer removeFromSuperlayer];
                }
                
            }
            
            
            self.layer.cornerRadius = originFrame.size.height/2;
            
            
            CABasicAnimation * radiusAnimation = [CABasicAnimation animationWithKeyPath:@"cornerRadius"];
            
            radiusAnimation.duration = 0.5f;
            
            radiusAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
            
            radiusAnimation.fromValue = @(_progressHight/2);
            
            radiusAnimation.delegate = self;
            
            [self.layer addAnimation:radiusAnimation forKey:@"cornerRadiusExpandAnim"];
            
            
            
        }];
        
        
    
    }

}




@end


